package incubator;

import java.awt.MenuBar;
import java.nio.FloatBuffer;

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.fixedfunc.GLLightingFunc;
import javax.media.opengl.fixedfunc.GLMatrixFunc;
import javax.media.opengl.glu.GLU;
import javax.media.opengl.glu.GLUquadric;

import templates.JOGLProjectTemplate;

/**
 * POC sur les lumières 3D
 * 
 * @author sebastien
 */
public class POCLight extends JOGLProjectTemplate {

	public POCLight() {
		super();
		canvas.addGLEventListener(this);
		start("POCLight");
	}

	@Override
	public void resetCamera() {}

	@Override
	public void enableLights() {
		gl.glEnable(GLLightingFunc.GL_LIGHTING);
		gl.glEnable(GLLightingFunc.GL_LIGHT0);
		gl.glEnable(GL.GL_DEPTH_TEST);
	}

	@Override
	protected void animate() {}

	@Override
	protected void createLights() {
		float[] lightPosition = { 10.0f, 10.0f, 10.0f, 0.0f };
		float[] lightColor = { 1.0f, 1.0f, 0.5f, 0.5f };
		FloatBuffer whiteLight = FloatBuffer.wrap(lightColor);

		float[] lightColorSpecular = { 0.5f, 1.0f, 0.5f, 0.5f };
		FloatBuffer whiteLightSpecular = FloatBuffer.wrap(lightColorSpecular);

		gl.glLightfv(GLLightingFunc.GL_LIGHT0, GLLightingFunc.GL_POSITION, lightPosition, 0);
		gl.glLightfv(GLLightingFunc.GL_LIGHT0, GLLightingFunc.GL_DIFFUSE, whiteLight);
		gl.glLightfv(GLLightingFunc.GL_LIGHT0, GLLightingFunc.GL_SPECULAR, whiteLightSpecular);
	}

	@Override
	protected void draw() {
		
		// Set material properties.
		float[] rgba = { 0.5f, 0.5f, 1f };
		GLUquadric planetQuadric = glu.gluNewQuadric();
		gl.glMaterialfv(GL.GL_FRONT, GL2.GL_AMBIENT, rgba, 0);
		gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, rgba, 0);
		gl.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, 0.5f);
		glu.gluQuadricDrawStyle(planetQuadric, GLU.GLU_FILL);
		glu.gluQuadricNormals(planetQuadric, GLU.GLU_FLAT);
		glu.gluQuadricOrientation(planetQuadric, GLU.GLU_OUTSIDE);
		glu.gluSphere(planetQuadric, 1.0, 30, 30);
		glu.gluDeleteQuadric(planetQuadric);
	}

	@Override
	public void dispose(GLAutoDrawable arg0) {}

	@Override
	public void reshape(GLAutoDrawable gLDrawable, int arg1, int arg2, int width, int height) {
		GL2 gl = gLDrawable.getGL().getGL2();
		if (height <= 0) {
			height = 1;
		}
		float h = (float) width / height;
		gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
		gl.glLoadIdentity();
		glu.gluPerspective(50.0f, h, 1.0, 1000.0);
		gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
		gl.glLoadIdentity();
	}

	@Override
	public MenuBar createMenuBar() {
		return null;
	}

	public static void main(String[] args) {
		new POCLight();
	}

}
